package extension.game.poker.handeval;

import java.util.ArrayList;
import java.util.Random;

public final class CardConverter {

    private static final int[] suitIndex = new int[]{2, 1, 3, 0};

    private static int toSuit(int index) {
        if (index > 51) {
            throw new IllegalArgumentException("Number '" + index + "' is not a valid card");
        }
        return suitIndex[index / 13];
    }

    public static Card toCard(int cardIndex) {
        return new Card(Card.Rank.values()[(cardIndex + 12) % 13],
                Card.Suit.values()[toSuit(cardIndex)]);
    }

    public static CardSet toCardSet(int[] cardSet) {
        CardSet result = new CardSet(cardSet.length);
        for (int i : cardSet) {
            result.add(toCard(i));
        }

        return result;
    }

    public static int[] randomCards(Random random, int n) {
        ArrayList<Integer> all = new ArrayList<Integer>(52);
        for (int i = 0; i < 52; i++) {
            all.add(new Integer(i));
        }
        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            res[i] = all.remove(random.nextInt(all.size())).intValue();
        }
        return res;
    }

    public static void main(String[] arg) {
//		CardSet cards;
//		Random r = new Random();
//		long t = System.nanoTime();
//		for (int i = 0; i < 500; i++) {
//			cards = CardConverter.toCardSet(CardConverter.randomCards(r, 7));
//			cards = CardConverter.toCardSet(new int[] {1, 2, 3, 4, 5, 9, 7});
//			int evalValue = HandEval.hand7Eval(HandEval.encode(cards)) | 0xe0000000;
//			System.out.println("eval " + cards.toString() + " = " + 
//					Integer.toHexString(evalValue).toUpperCase()
//					)
//					;
//		}
        System.out.println(new TexasHoldemHandResult(new int[]{0, 32, 22, 12, 3, 7}).toString());
//		t = System.nanoTime() - t;
//		System.out.println(t / 1000000);
    }
}
